package com.gdones.gpt.bus.activecode.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.gdones.common.core.annotations.MySysLog;
import com.gdones.common.core.annotations.MySysLogOpration;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyResult;
import com.gdones.gpt.bus.activecode.dto.ActiveCodeDto;
import com.gdones.gpt.bus.activecode.dto.GptActivationCode;
import com.gdones.gpt.bus.activecode.service.IGptActivationCodeService;
import com.gdones.gpt.bus.modetemplate.dto.GptTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-06-03
 */
@RestController
@RequestMapping("/activecode")
@Slf4j
@Api(tags = "激活码管理")
public class GptActivationCodeController {

    @Resource(name = "gptActivationCodeServiceImpl")
    private IGptActivationCodeService gptActivationCodeServiceImpl;

    @GetMapping("/pages")
    @ApiOperation("激活码管理-分页")
    @MySysLog(modelName="激活码管理",methodDesc = "分页",methodType = MySysLogOpration.GET)
    public MyResult pages(ActiveCodeDto dto, MyPageBand pageBand){

        log.debug("--激活码管理：分页");
        MyResult result = new MyResult();

        try {
            result = gptActivationCodeServiceImpl.pages(dto,pageBand);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("激活码管理-分页-异常");
        }
        return result;
    }

    @GetMapping("/list")
    @ApiOperation("激活码管理-列表不分页")
    @MySysLog(modelName="激活码管理",methodDesc = "列表不分页",methodType = MySysLogOpration.GET)
    public MyResult list(ActiveCodeDto dto ){

        log.debug("--激活码管理：列表不分页");
        MyResult result = new MyResult();

        try {
            result = gptActivationCodeServiceImpl.mylist(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("激活码管理-列表不分页-异常");
        }
        return result;
    }

    @GetMapping("/listExport")
    @ApiOperation("激活码管理-列表不分页-导出")
    @MySysLog(modelName="激活码管理",methodDesc = "列表不分页-导出",methodType = MySysLogOpration.GET)
    public MyResult listExport(ActiveCodeDto dto, HttpServletResponse response){

        log.debug("--激活码管理：列表不分页-导出");
        MyResult result = new MyResult();

        try {
            result = gptActivationCodeServiceImpl.mylist(dto);

            // 进行导出
            List<ActiveCodeDto> list = (List<ActiveCodeDto>) result.getData();
            if(list.size()<=0){
                result.setCode("9999");
                result.setMsg("暂无任何数据可以导出！");
                return result;
            }

            Map<String, String> map = MapUtil.newHashMap(true); // 重点
            map.put("activeCode", "卡号");
            map.put("temp1", "密码");
            map.put("activeDays", "激活天数");
            map.put("comment", "备注");

            ArrayList<ActiveCodeDto> rows = CollUtil.newArrayList(list);

            // 通过工具类创建writer，默认创建xls格式
            ExcelWriter writer = ExcelUtil.getWriter();

            if (map != null) {
                map.forEach((key, value) -> {
                    writer.addHeaderAlias(key, value);
                });
            }
//创建xlsx格式的
//ExcelWriter writer = ExcelUtil.getWriter(true);
// 一次性写出内容，使用默认样式，强制输出标题
            writer.write(rows, true);
//out为OutputStream，需要写出到的目标流
            ServletOutputStream outputStream = response.getOutputStream();
            writer.flush(outputStream);
// 关闭writer，释放内存
            writer.close();

        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("激活码管理-列表不分页-导出-异常");
        }
        return result;
    }




    @GetMapping("/getByID")
    @ApiOperation("激活码管理-查询模型详情")
    @MySysLog(modelName="激活码管理",methodDesc = "查询模型详情",methodType = MySysLogOpration.GET)
    public MyResult getByID(String id){

        log.debug("--激活码管理：查询模型详情");
        MyResult result = new MyResult();

        try {
            result = gptActivationCodeServiceImpl.getByID(id);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("激活码管理-查询模型详情-异常");
        }
        return result;
    }

    @PostMapping("/createAcitveCodes")
    @ApiOperation("激活码管理-通过模板批量开卡")
    @MySysLog(modelName="激活码管理",methodDesc = "通过模板批量开卡",methodType = MySysLogOpration.POST)
    public MyResult createAcitveCodes(@RequestBody ActiveCodeDto dto){

        log.debug("--激活码管理：通过模板批量开卡");
        MyResult result = new MyResult();

        try {
            result = gptActivationCodeServiceImpl.createAcitveCodes(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("激活码管理-通过模板批量开卡-异常");
        }
        return result;
    }

    @PutMapping("/staffActiveTheCode")
    @ApiOperation("激活码管理-用户使用激活码")
    @MySysLog(modelName="激活码管理",methodDesc = "用户使用激活码",methodType = MySysLogOpration.PUT)
    public MyResult staffActiveTheCode(@RequestBody GptActivationCode dto){

        log.debug("--激活码管理：用户使用激活码");
        MyResult result = new MyResult();

        try {
            result = gptActivationCodeServiceImpl.staffActiveTheCode(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("激活码管理-用户使用激活码-异常");
        }
        return result;
    }

    @PutMapping("/staffActiveTiYan")
    @ApiOperation("激活码管理-激活用户体验版")
    @MySysLog(modelName="激活码管理",methodDesc = "激活用户体验版",methodType = MySysLogOpration.PUT)
    public MyResult staffActiveTiYan(String staffID){

        log.debug("--激活码管理：激活用户体验版");
        MyResult result = new MyResult();

        try {
            result = gptActivationCodeServiceImpl.staffActiveTiYan(staffID);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("激活码管理-激活用户体验版-异常");
        }
        return result;
    }


    @DeleteMapping("/deletes")
    @ApiOperation("激活码管理-删除")
    @MySysLog(modelName="激活码管理",methodDesc = "删除",methodType = MySysLogOpration.DELETE)
    public MyResult deletes(String ids){

        log.debug("--激活码管理：删除");
        MyResult result = new MyResult();

        try {
            result = gptActivationCodeServiceImpl.deletes(ids);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("激活码管理-删除-异常");
        }
        return result;
    }
    
}
